/**
 * @author        hejiyang <silentmanager@163.com>
 * @date          2024-11-11 23:26:19
 * Copyright © YourCompanyName All rights reserved
 */
// 常用验证规则封装

export const rules = {
  required: (message: string) => ({
    required: true,
    message,
    trigger: ['blur', 'change']
  }),

  minLength: (minLength: number, message: string) => ({
    min: minLength,
    message,
    trigger: ['blur', 'change']
  }),

  maxLength: (maxLength: number, message: string) => ({
    max: maxLength,
    message,
    trigger: ['blur', 'change']
  }),

  email: {
    type: 'email',
    message: '请输入有效的邮箱地址',
    trigger: ['blur', 'change']
  },

  phone: {
    pattern: /^[0-9]{10}$/, // 可以根据需求修改为更复杂的正则
    message: '请输入有效的手机号',
    trigger: ['blur', 'change']
  },

  number: {
    type: 'number',
    message: '请输入数字',
    trigger: ['blur', 'change']
  },

  passwordStrength: {
    validator: (rule: any, value: string, callback: Function) => {
      const reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/ // 至少8个字符，包含字母和数字
      if (!value) {
        callback(new Error('请输入密码'))
      } else if (!reg.test(value)) {
        callback(new Error('密码至少8个字符，包含字母和数字'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  },

  noSpecialChar: {
    validator: (rule: any, value: string, callback: Function) => {
      const reg = /^[A-Za-z0-9]+$/ // 仅字母和数字
      if (value && !reg.test(value)) {
        callback(new Error('请输入有效的内容，不能包含特殊字符'))
      } else {
        callback()
      }
    },
    trigger: ['blur', 'change']
  }
}
